package com.jh.jingfei.bottom_bar;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.jh.jingfei.R;
import com.jh.jingfei.utils.DensityUtil;

/**
 * @author lxb
 * Created by sxb on 2017/7/19.
 */

public class BottomNavigationBar extends LinearLayout {

    private int selectedIndex;

    private BottomNavigationTab[] tabs = new BottomNavigationTab[2];

    private OnTabSelectedListener onTabSelectedListener;

    public BottomNavigationBar(Context context) {
        this(context, null);
    }

    public BottomNavigationBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public BottomNavigationBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public BottomNavigationBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.setOrientation(HORIZONTAL);
    }

//    private void createTabs() {
//        int w = items.length;
//        this.setWeightSum(w);
//        for (int i = 0; i < items.length; i++) {
//            createTab(items[i]);
//        }
//    }
//
//    private void createTab(BottomNavigationItem item) {
//        BottomNavigationTab tab = new BottomNavigationTab(getContext());
//        tab.setLayoutParams(generateLp());
//        tab.setBadge(item.getBadge());
////        tab.setIconDrawable(item.);
////        tab.setBackgroundResource(android.R.attr.selectableItemBackgroundBorderless);
//
//        int leftRight = DensityUtil.dp2px(getContext(), 12);
//        int bottom = DensityUtil.dp2px(getContext(), 10);
//        int top = DensityUtil.dp2px(getContext(), 2);
//        tab.setPadding(leftRight, top, leftRight, bottom);
//    }
//
//    private LinearLayout.LayoutParams generateLp() {
//        LinearLayout.LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
//        lp.weight = 1;
//        return  lp;
//    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initTabs();
    }

    private void initTabs() {
        tabs[0] = findViewById(R.id.tab_home);
        tabs[1] = findViewById(R.id.tab_rank);

        tabs[0].setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                select(0);
            }
        });
        tabs[1].setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                select(1);
            }
        });
    }

    private void select(int position) {
        if (selectedIndex == position) {
            if (onTabSelectedListener != null) {
                onTabSelectedListener.onTabReselected(position);
            }
        } else {
            if (onTabSelectedListener != null) {
                onTabSelectedListener.onTabSelected(position);
                onTabSelectedListener.onTabUnselected(selectedIndex);
            }
            tabs[position].active();
            tabs[selectedIndex].inactive();
            selectedIndex = position;
        }
    }

    public BottomNavigationTab getTab(int position) {
        return tabs[position];
    }

    public void setOnTabSelectedListener(OnTabSelectedListener onTabSelectedListener) {
        this.onTabSelectedListener = onTabSelectedListener;
    }

    public interface OnTabSelectedListener {
        void onTabSelected(int position);
        void onTabUnselected(int position);
        void onTabReselected(int position);
    }

    public static class SimpleOnTabSelectedListener implements OnTabSelectedListener{
        @Override
        public void onTabSelected(int position) {

        }

        @Override
        public void onTabUnselected(int position) {

        }

        @Override
        public void onTabReselected(int position) {

        }
    }

    private OnCenterBtnClickListener onCenterBtnClickListener;

    public interface OnCenterBtnClickListener {
        void onClick(View v);
    }

    public void setOnCenterBtnClickListener(OnCenterBtnClickListener onCenterBtnClickListener) {
        this.onCenterBtnClickListener = onCenterBtnClickListener;
    }
}
